package com.dataStructures.tree;

public class ArrBinaryTreeDemo {
	public static void main(String[] args) {
		int[] arr = {1, 2, 3, 4, 5, 6, 7};
		ArrBinaryTree tree = new ArrBinaryTree(arr);
		tree.preOrder();
	}
}

/**
 * 实现顺序存储二叉树遍历
 */
class ArrBinaryTree {
	private int[] arr;

	public ArrBinaryTree(int[] arr) {
		this.arr = arr;
	}

	public void preOrder() {
		this.preOrder(0);
	}

	private void preOrder(int index) {
		if (arr == null || arr.length == 0) {
			System.out.println("arr empty");
		}
		// 输出当前数据
		System.out.println(arr[index]);
		// 向左递归遍历
		int leftIndex = index * 2 + 1;
		if (leftIndex < arr.length) {
			preOrder(leftIndex);
		}
		// 向右递归遍历
		int rightIndex = index * 2 + 2;
		if (rightIndex < arr.length) {
			preOrder(rightIndex);
		}
	}
}
